#include <Bitmap.hpp>
#include <Ray.hpp>
#include <glm/gtc/matrix_transform.hpp>

int main(int argc, const char **argv) {

  uint32_t width = 400;
  uint32_t height = 300;

  Bitmap bitmap(width, height);

  auto viewport_height = 2.0;
  auto viewport_width = (4.0 / 3.0) * viewport_height;
  auto focal_length = 1.0;

  glm::vec3 origin(0.f, 0.f, 0.f);

  glm::mat4 perspect = glm::ortho<float>(0.f, width, 0.f, height);

  auto horizontal = glm::vec3(viewport_width, 0, 0);
  auto vertical = glm::vec3(0, viewport_height, 0);
  auto lower_left_corner = origin - horizontal / 2.f - vertical / 2.f -
                           glm::vec3(0, 0, focal_length);

  for (uint32_t i = 0; i < bitmap.Width(); i++) {
    for (uint32_t j = 0; j < bitmap.Height(); j++) {
      auto u = float(i) / (width - 1);
      auto v = float(j) / (height - 1);

      Ray ray(
          origin,
          glm::normalize(perspect * glm::vec4{glm::vec3(i, j, focal_length) - glm::vec3(width / 2.f, height / 2.f, 0.f), 0.f}));
      // Ray ray(origin,
      //        lower_left_corner + u * horizontal + v * vertical - origin);

      auto color = Ray::RayColor(ray);

      bitmap.setPixel(i, height - 1 -j, color);
    }
  }

  bitmap.writeToFile("sending_ray2.png");
  return 0;
}